Data Vectors
The Plane
Pairs of numbers can be depicted as points on a plane.
The plane is normally denoted by .
Details
Pairs of numbers can be depicted as points on a plane.
A plane is a perfectly flat surface with no thickness and no end, it can extend forever in all directions. It has two-dimensions, length and width. We need two values to find a point on the plane.
Normally we talk about "the plane" (or "the -plane") as the collection of all pairs of numbers and denote it by
giving coordinates to each point. The plane is also sometimes called The Cartesian coordinate system, named after its inventor, the French polymath René Descartes.
Examples
Plotting the point in the -plane using R:
plot(2,4,xlim=c(0,6),ylim=c(0,6),xlab="x",ylab="y",cex=2)
text(2,4,"(2,4)",pos=4,cex=2)
Additional points can be added using the points()
function:
points(3,5, cex = 0.5) ## a point at (3,5)
If you have two sets of coordinates on a plane you, can calculate the distance between the two points and graph the line connecting the points.
What is the distance between the two points and ? What is the distance between the 2 points (3,9) and (5,1)?
We will use the Pythagorean theorem:
We insert our values into the formula:
When we combine inside the parentheses we get:
Squaring both terms:
Then we take the square root:
The result:
Simple Plots in R
Graphing functions in R
plot()
- plots a scatter plot (as a line plot)points()
- adds points to a plottext()
- adds text to a plotlines()
- adds lines to a plot
Figure: Points on a plane, drawn with R.
Examples
plot(2,3)
gives a single plot and
plot(2,3, xlim=c(0,5), ylim=c(0,5))
gives a single plot but forces both axes to range from 0
to 5
.
The following R commands can be used to generate a plot with two points:
plot(1,2,xlim=c(0,5),ylim=c(0,5),xlab="x",ylab="y")
points(3,1)
text(1,2,"(1,2)",pos=4, cex=2)
text(3,1,"(3,1)",pos=4, cex=2)
In this example, we plot three points. The first two arguments of the plot function. The third plot was added with the points are by including vectors with a length of as the and arguments of the plot function. The third point was added with the points function. The second and third points were labeled using the text function and a line was drawn between them using the lines function.
plot(c(2,3),c(3,4),xlim=c(2,6),ylim=c(1,5),xlab="x",ylab="y")
points(4,2)
text(3,4,"(3,4)",pos=4, cex=2)
text(4,2,"(4,2)",pos=4, cex=2)
lines(c(3,4), c(4,2))
Note: Note that if you are unsure of what format the arguments of an R function needs to be, you can call a help file by typing ?
before the function name (e.g. ?lines
).
Data
Data are usually a sequence of numbers, typically called a vector.
Details
When we collect data these are one or more sequences of numbers, collected into data vectors. We commonly think of these data vectors as columns in a table.
Examples
In R, if the command
x <- c(4,5,3,7)
is given, then x
contains a vector of numbers.
Create a function in R, give it a name Myfunction
which takes the sum of x
and y
:
Myfunction <- function(x,y) { sum(x,y) }
If you input the vectors 1:3
and 4:7
into the function it will calculate the sum of x <- (1+2+3)
and y <- (4+5+6+7)
as follows:
> Myfunction(1:3,4:7)
[1] 28
Indices for a Data Vector
If data are in a vector x
, then we use indices to refer to individual elements.
Details
If i
is an integer then denotes the element of .
Note: Although we do not distinguish (much) between row- and column vectors, usually a vector is thought of as a column. If we need to specify the type of vector, row or column, then for vector , the column vector would be referred to as and the row vector as .
(the transpose of the original).
Examples
If then and
How to remove all indices below a certain value in R?
> x <- c(1,5,8,9,4,16,12,7,11)
> x
[1] 1 5 8 9 4 16 12 7 11
> y <- x[x>10]
> y
[1] 16 12 11
Consider a function that takes to vectors
as arguments with:
and:
The function returns the sum:
Long version:
> fn <- function(a,b) {
+ result <- sum(a[b])
+ return(result)
+ }
Short version:
fN <- function(a,b) sum(a[b])
Summation
We use the symbol to denote sums.
In R, the sum function adds numbers.
Examples
If
then
and
In R one can give the corresponding commands:
> x <- c(4,5,3,7)
> x
[1] 4 5 3 7
> sum(x)
[1] 19
> sum(x[2:4])
[1] 15